import 'package:container_tab_indicator/container_tab_indicator.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:xvideos/hj_utils/holiday_theme.dart';

class CommunitySortHeader extends StatefulWidget {
  final Function(int sort) sortOnTap;
  final Function(TabController controlelr)? onInit;
  final TabController? controller;
  final bool isInTop; //
  const CommunitySortHeader({
    super.key,
    required this.sortOnTap,
    this.onInit,
    this.controller,
    this.isInTop = false,
  });

  @override
  State<CommunitySortHeader> createState() => _CommunitySortHeaderState();
}

class _CommunitySortHeaderState extends State<CommunitySortHeader> with SingleTickerProviderStateMixin {
  final sorts = ['最多收藏', '最新上架', '最多观看'];
  late TabController _controller;

  @override
  void initState() {
    super.initState();

    if (widget.controller != null) {
      _controller = widget.controller!;
    } else {
      _controller = TabController(length: sorts.length, vsync: this);
    }
    if (widget.onInit != null) {
      widget.onInit?.call(_controller);
    }
  }

  @override
  Widget build(BuildContext context) {
    Color bgColor = Theme.of(context).scaffoldBackgroundColor;
    if (holidayTheme.openHoliday && !widget.isInTop) {
      bgColor = Colors.transparent;
    }
    return Container(
      decoration: BoxDecoration(
        color: Color(0x0077FFFF),
        gradient: (widget.isInTop && holidayTheme.openHoliday)
            ? LinearGradient(
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter,
                colors: [
                  Color(0xff3e161c),
                  Color(0xff26161c),
                ],
              )
            : null,
      ),
      child: TabBar(
        isScrollable: true,
        labelPadding: EdgeInsets.symmetric(horizontal: 12.w),
        padding: EdgeInsets.zero,
        onTap: widget.sortOnTap,
        tabAlignment: TabAlignment.center,
        tabs: sorts
            .map((e) => Padding(
                  padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 4.h),
                  child: Text(e),
                ))
            .toList(),
        indicatorWeight: 1,
        unselectedLabelStyle: TextStyle(color: Colors.white.withOpacity(.6), fontSize: 16.sp, height: 1),
        labelStyle: TextStyle(color: Colors.white, fontSize: 16.sp, height: 1, fontWeight: FontWeight.w600),
        indicator: ContainerTabIndicator(
          padding: EdgeInsets.only(bottom: 1),
          width: 60,
          height: 30,
          color: Color(0x00161E2C),
          radius: BorderRadius.all(Radius.circular(25)),
          borderColor: Color(0x00000000),
        ),
        controller: _controller,
      ),
    );
  }
}
